import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import etxt2db.api.ClassificationEvaluator;
import etxt2db.api.ClassificationExecutor;
import etxt2db.api.ClassificationModel;
import etxt2db.api.ClassificationModelCreator;
import etxt2db.api.ClassificationExecutor.ClassifiedSpan;
import etxt2db.features.CharacterFeatureClassifier;
import etxt2db.features.CharacterTypeFeatureClassifier;
import etxt2db.features.ClassificationModelFeatureClassifier;
import etxt2db.features.EditableTokenFE;
import etxt2db.features.PatternFeatureClassifier;
import etxt2db.features.RegexFeatureClassifier;
import etxt2db.features.ValueCaseInsensitiveFeatureClassifier;
import etxt2db.features.ValueCaseSensitiveFeatureClassifier;
import etxt2db.serialization.ClassificationModelSerializer;


public class SolutionCtxt2dbCRF {
	public static void main(String[] args) throws IOException, ParseException{
		ClassificationModelSerializer serial = new ClassificationModelSerializer();
		ClassificationModelCreator trainer = new ClassificationModelCreator();
		
		List<String> types = new ArrayList<String>();
		types.add("SPO");
		//types.add("location");
		//types=null;
		
		List<CharacterFeatureClassifier> listFE = new ArrayList<CharacterFeatureClassifier>();
		listFE.add(new CharacterTypeFeatureClassifier());
		listFE.add(new PatternFeatureClassifier());
		listFE.add(new ValueCaseSensitiveFeatureClassifier());
		listFE.add(new ValueCaseInsensitiveFeatureClassifier());
		listFE.add(new RegexFeatureClassifier("Location|location|Location:|location:"));
		listFE.add(new RegexFeatureClassifier("[Rr]oom"));
		listFE.add(new ClassificationModelFeatureClassifier(serial.deserializeClassificationModel("/home/goncalo/Dropbox/Etxt2db/Etxt2db/annotators/HMMPOSTagger.ann"), "POS"));
		
		EditableTokenFE fe = new EditableTokenFE(listFE);
		
		File trainingFile = new File("/home/gsimoes/Dropbox/Etxt2db/ACEDatasetProcessor/ACE2005");
		
		File testingFile = new File("/home/gsimoes/Dropbox/Etxt2db/ACEDatasetProcessor/ACE2005Untagged");
		
		ClassificationModel model = trainer.trainMachineLearningModel(trainingFile,ClassificationModelCreator.MLTechnique.CRF,types);
		
		serial.serializeClassificationModel(model, "/home/gsimoes/Dropbox/Etxt2db/CIMPLEPrototypeTuples/models/extractSportOrganizationsCRF.bin");
		
		//ClassificationModel model = serial.deserializeClassificationModel("/home/goncalo/Dropbox/Etxt2db/CIMPLEPrototypeTuples/models/extractWeaponsCRF.bin");
		
		
		ClassificationExecutor exec = new ClassificationExecutor();
		
		System.out.println(exec.getClassifiedSegments(testingFile, model, types));
	}
	
	public static String getFileContent(String path) throws IOException{
		BufferedReader in = new BufferedReader(new FileReader(path));
		StringBuffer result = new StringBuffer();
		String input;
		while ((input = in.readLine()) != null) {
			result.append(input + "\n");
		}
		return result.toString();
	}
}
